/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import com.hvtt.web.models.RPT_TongDoanhThuModel;
import com.hvtt.web.utilities.Utilities;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.TreeNode;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.LineChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class RPT_TongDoanhThuController  implements Serializable {
    
     private WEBConnection DAL = WEBConnection.getInstance();
      ReportEcxelController controller=new ReportEcxelController();
    private StreamedContent fileExporter;
       private String cpid = "";
    private String hdh = "";
    private String gameID = "";
    private String congTT = "";
    private String telco = "";
    private String loaiThongKe = "";
    private Date tuNgay = Calendar.getInstance().getTime();
    private Date denNgay = new java.sql.Date(0);
    private long giaTriTheNap = 0;
    private String page = "";
    private String loaiBD = "";
    
    private double tong1=0;
    private double tong2=0;
    private double tong3=0;
    private double tong4=0;
    
    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    /**
     * @return the cpid
     */
    public String getCpid() {
        return cpid;
    }

    /**
     * @param cpid the cpid to set
     */
    public void setCpid(String cpid) {
        this.cpid = cpid;
    }

    /**
     * @return the hdh
     */
    public String getHdh() {
        return hdh;
    }

    /**
     * @param hdh the hdh to set
     */
    public void setHdh(String hdh) {
        this.hdh = hdh;
    }

    /**
     * @return the gameID
     */
    public String getGameID() {
        return gameID;
    }

    /**
     * @param gameID the gameID to set
     */
    public void setGameID(String gameID) {
        this.gameID = gameID;
    }

    /**
     * @return the congTT
     */
    public String getCongTT() {
        return congTT;
    }

    /**
     * @param congTT the congTT to set
     */
    public void setCongTT(String congTT) {
        this.congTT = congTT;
    }

    /**
     * @return the telco
     */
    public String getTelco() {
        return telco;
    }

    /**
     * @param telco the telco to set
     */
    public void setTelco(String telco) {
        this.telco = telco;
    }

    /**
     * @return the loaiThongKe
     */
    public String getLoaiThongKe() {
        return loaiThongKe;
    }

    /**
     * @param loaiThongKe the loaiThongKe to set
     */
    public void setLoaiThongKe(String loaiThongKe) {
        this.loaiThongKe = loaiThongKe;
    }

    /**
     * @return the tuNgay
     */
    public Date getTuNgay() {
        return tuNgay;
    }

    /**
     * @param tuNgay the tuNgay to set
     */
    public void setTuNgay(Date tuNgay) {
        this.tuNgay = tuNgay;
    }

    /**
     * @return the denNgay
     */
    public Date getDenNgay() {
        return denNgay;
    }

    /**
     * @param denNgay the denNgay to set
     */
    public void setDenNgay(Date denNgay) {
        this.denNgay = denNgay;
    }

    /**
     * @return the giaTriTheNap
     */
    public long getGiaTriTheNap() {
        return giaTriTheNap;
    }

    /**
     * @param giaTriTheNap the giaTriTheNap to set
     */
    public void setGiaTriTheNap(long giaTriTheNap) {
        this.giaTriTheNap = giaTriTheNap;
    }

    /**
     * @return the loaiBD
     */
    public String getLoaiBD() {
        return loaiBD;
    }

    /**
     * @param loaiBD the loaiBD to set
     */
    public void setLoaiBD(String loaiBD) {
        this.loaiBD = loaiBD;
    }

    /**
     * @return the tong1
     */
    public double getTong1() {
        return tong1;
    }

    /**
     * @param tong1 the tong1 to set
     */
    public void setTong1(double tong1) {
        this.tong1 = tong1;
    }

    /**
     * @return the tong2
     */
    public double getTong2() {
        return tong2;
    }

    /**
     * @param tong2 the tong2 to set
     */
    public void setTong2(double tong2) {
        this.tong2 = tong2;
    }

    /**
     * @return the tong3
     */
    public double getTong3() {
        return tong3;
    }

    /**
     * @param tong3 the tong3 to set
     */
    public void setTong3(double tong3) {
        this.tong3 = tong3;
    }

    /**
     * @return the tong4
     */
    public double getTong4() {
        return tong4;
    }

    /**
     * @param tong4 the tong4 to set
     */
    public void setTong4(double tong4) {
        this.tong4 = tong4;
    }
    
    
    public TreeNode RPT_ThongKeDoanhThu_BAC_ThongKe() throws SQLException {
        TreeNode root = new DefaultTreeNode(new RPT_TongDoanhThuModel(), null);
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_TongDoanhThuModel> lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);

        int i = 0;
        double lTong1=0;
        double lTong2=0;
        double lTong3=0;
        
        String strCPID;
        while (i < lst.size()) {
            RPT_TongDoanhThuModel m = new RPT_TongDoanhThuModel();
            m.setCpName(lst.get(i).getCpName());
            TreeNode noteParent = new DefaultTreeNode(m, root);
            noteParent.setExpanded(true);


            strCPID = lst.get(i).getCpid();

            int iCountSoUser = 0;
            double lCountSoBIT = 0;
            double lPT=0;

            if (i < lst.size()) {
                while (i < lst.size() && (lst.get(i).getCpid() == null ? strCPID == null : lst.get(i).getCpid().equals(strCPID))) {
                    lst.get(i).setCpName("");
                    lst.get(i).setPT((float)lst.get(i).getTongSoBit() * 20 / 100);
                    noteParent.getChildren().add(new DefaultTreeNode(lst.get(i)));
                    iCountSoUser += lst.get(i).getSoNguoiChoi();
                    lCountSoBIT += lst.get(i).getTongSoBit();
                    lTong1+=lst.get(i).getSoNguoiChoi();
                    lTong2+=lst.get(i).getTongSoBit();
                    lTong3+=lst.get(i).getPT();
                    lPT+=lst.get(i).getPT();
                    m.setGame(lst.get(i).getGame());
                    i++;
                }
                m.setSoNguoiChoi(iCountSoUser);
                m.setTongSoBit(lCountSoBIT);
                m.setPT((float)lPT);
                m.setGame("");

            }
            // i++;
        }
        this.setTong1(lTong1);
        this.setTong2(lTong2);
        this.setTong3(lTong3);
        return root;
    }

    public TreeNode RPT_ThongKeDoanhThu_BIT_ThongKe() throws SQLException {
        TreeNode root = new DefaultTreeNode(new RPT_TongDoanhThuModel(), null);
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_TongDoanhThuModel> lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);
        double lTong1=0;
        double lTong2=0;
        double lTong3=0;
        
        int i = 0;
        String strCPID;
        while (i < lst.size()) {
            RPT_TongDoanhThuModel m = new RPT_TongDoanhThuModel();
            m.setCpName(lst.get(i).getCpName());
            TreeNode noteParent = new DefaultTreeNode(m, root);
            noteParent.setExpanded(true);


            strCPID = lst.get(i).getCpid();

            int iCountSoUser = 0;
            long lCountSoBIT = 0;
            double dblPTBit=0;

            if (i < lst.size()) {
                while (i < lst.size() && (lst.get(i).getCpid() == null ? strCPID == null : lst.get(i).getCpid().equals(strCPID))) {
                    lst.get(i).setCpName("");
                    if(Sys_NguoiDungController.findUser().isIsAdmin())
                    {
                        lst.get(i).setPT((float)lst.get(i).getTongSoBit() * 2 / 100);
                        noteParent.getChildren().add(new DefaultTreeNode(lst.get(i)));
                        iCountSoUser += lst.get(i).getSoNguoiChoi();
                        lCountSoBIT += lst.get(i).getTongSoBit();
                        dblPTBit+=lst.get(i).getTongSoBit() * 2 / 100;
    
                        lTong1+=lst.get(i).getSoNguoiChoi();
                        lTong2+=lst.get(i).getTongSoBit();
                        lTong3+=lst.get(i).getTongSoBit() * 2 / 100;
                        m.setGame(lst.get(i).getGame());
                    }
                    else
                    {
                        lst.get(i).setPT((float)(lst.get(i).getTongSoBit() * 2 / 100)*30/100);
                        noteParent.getChildren().add(new DefaultTreeNode(lst.get(i)));
                        iCountSoUser += lst.get(i).getSoNguoiChoi();
                        lCountSoBIT += lst.get(i).getTongSoBit();

                        lTong1+=lst.get(i).getSoNguoiChoi();
                        lTong2+=lst.get(i).getTongSoBit();
                        dblPTBit+=(lst.get(i).getTongSoBit() * 2 / 100)*30/100;
                        lTong3+=(lst.get(i).getTongSoBit() * 2 / 100)*30/100;
                        m.setGame(lst.get(i).getGame());
                    }
                    i++;
                }
                m.setSoNguoiChoi(iCountSoUser);
                m.setTongSoBit(lCountSoBIT);
                 m.setPT((float)dblPTBit);
                

            }
            // i++;
        }
        this.setTong1(lTong1);
        this.setTong2(lTong2);
        this.setTong3(lTong3);
        return root;
    }
    
    public TreeNode RPT_ThongKeDoanhThu_ThongKe() throws SQLException {
        TreeNode root = new DefaultTreeNode(new RPT_TongDoanhThuModel(), null);
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_TongDoanhThuModel> lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);
        double lTong1=0;
        double lTong2=0;
        double lTong3=0;
        double lTong4=0;
        
        int i = 0;
        String strCPID;
        while (i < lst.size()) {
            RPT_TongDoanhThuModel m = new RPT_TongDoanhThuModel();
            m.setCpName(lst.get(i).getCpName());
            TreeNode noteParent = new DefaultTreeNode(m, root);
            noteParent.setExpanded(true);

           // long ltongDoanhThu=0;
            strCPID = lst.get(i).getCpid();

            double lTongPTBit = 0;
            double lTongPTBac = 0;
            
         

            if (i < lst.size()) {
                while (i < lst.size() && (lst.get(i).getCpid() == null ? strCPID == null : lst.get(i).getCpid().equals(strCPID))) {
                    lst.get(i).setCpName("");
                    //lst.get(i).setPT(lst.get(i).getTongSoBit() * 2 / 100);
                    noteParent.getChildren().add(new DefaultTreeNode(lst.get(i)));
                   // iCountSoUser += lst.get(i).getSoNguoiChoi();
                    lTongPTBit += lst.get(i).getPtBit();
                    lTongPTBac += lst.get(i).getTongSoBit();
                    
                   
                    
                    lTong1+=lst.get(i).getPtBit();
                    lTong2+=lst.get(i).getTongSoBit();
                    lTong3+=lTong1+lTong2;
                   
                    m.setGame(lst.get(i).getGame());
                    i++;
                }
              m.setPtBit(lTongPTBit);
              m.setTongSoBit(lTongPTBac);
              

            }
            // i++;
        }
        this.setTong1(lTong1);
        this.setTong2(lTong2);
        this.setTong3(lTong3);
        this.setTong4(lTong4);
        return root;
    }
    
     public TreeNode RPT_ThongKeDoanhThu_ThongKe_CP() throws SQLException {
        TreeNode root = new DefaultTreeNode(new RPT_TongDoanhThuModel(), null);
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_TongDoanhThuModel> lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);
        double lTong1=0;
        double lTong2=0;
        double lTong3=0;
        double lTong4=0;
        
        int i = 0;
        String strCPID;
        while (i < lst.size()) {
            RPT_TongDoanhThuModel m = new RPT_TongDoanhThuModel();
            m.setCpName(lst.get(i).getCpName());
            TreeNode noteParent = new DefaultTreeNode(m, root);
            noteParent.setExpanded(true);

            double ltongDoanhThu=0;
            strCPID = lst.get(i).getCpid();

            double lTongPTBit = 0;
            double lTongPTBac = 0;
            
         

            if (i < lst.size()) {
                while (i < lst.size() && (lst.get(i).getCpid() == null ? strCPID == null : lst.get(i).getCpid().equals(strCPID))) {
                    lst.get(i).setCpName("");
                    //lst.get(i).setPT(lst.get(i).getTongSoBit() * 2 / 100);
                    RPT_TongDoanhThuModel m_child=lst.get(i);
                   
                   // iCountSoUser += lst.get(i).getSoNguoiChoi();
                    lTongPTBit += m_child.getPtBit()*30/100;
                    lTongPTBac += m_child.getPtBac();
                    ltongDoanhThu+=m_child.getPtBit()*30/100+m_child.getPtBac();
                    
                   
                    
                    lTong1+=m_child.getPtBit()*30/100;                            
                    lTong2+=m_child.getPtBac();
                    lTong3+=m_child.getPtBit()*30/100+m_child.getPtBac();
                    
                    m_child.setDoanhThuCP(m_child.getPtBit()*30/100+m_child.getPtBac());
                    
                    m_child.setPtBit(m_child.getPtBit()*30/100);
                    
                 //  m_child.setPtBit((lst.get(i).getTongSoBit()*2/100)*30/100);
                   // m.setPtBit(m_child.getPtBit()*30/100);
                    m.setGame(m_child.getGame());
                    
                     noteParent.getChildren().add(new DefaultTreeNode(m_child));
                    i++;
                }
              m.setPtBac(lTongPTBac);
              m.setPtBit(lTongPTBit);
              m.setDoanhThuCP(ltongDoanhThu);

            }
        }
        this.setTong1(lTong1);
        this.setTong2(lTong2);
        this.setTong3(lTong3);
        this.setTong4(lTong4);
        return root;
    }
    
    

    public PieChartModel RPT_ThongKeDoanhThu_Chart_BIT() throws SQLException {
        PieChartModel c = new PieChartModel();
        List<RPT_TongDoanhThuModel> lst;

        
        // taikhoans.setLabel("X: ngày, Y: số tài khoản");
        //ChartSeries hdhs = new ChartSeries();

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu_Chart_BIT(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);


        for (RPT_TongDoanhThuModel m : lst) {
            if(Sys_NguoiDungController.findUser().isIsAdmin())
                c.set(m.getCpName() + " "+ Utilities.formatNumber("#,###", m.getTongSoBit()), m.getTongSoBit());
            else
                c.set(m.getCpName() + " "+ Utilities.formatNumber("#,###", m.getTongSoBit()*30/100), m.getTongSoBit()*30/100);
        }
        return c;
    }
    public PieChartModel RPT_ThongKeDoanhThu_Chart_BAC() throws SQLException {
        PieChartModel c = new PieChartModel();
        List<RPT_TongDoanhThuModel> lst;

        
        // taikhoans.setLabel("X: ngày, Y: số tài khoản");
        //ChartSeries hdhs = new ChartSeries();

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu_Chart_BAC(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);

        
        for (RPT_TongDoanhThuModel m : lst) {
           c.set(m.getCpName() +" "+ Utilities.formatNumber("#,###", m.getTongSoBit()), m.getTongSoBit());
        }
        return c;
    }
    public PieChartModel RPT_ThongKeDoanhThu_Chart_DoanhThuTong() throws SQLException {
        PieChartModel c = new PieChartModel();
        List<RPT_TongDoanhThuModel> lst;

        
        // taikhoans.setLabel("X: ngày, Y: số tài khoản");
        //ChartSeries hdhs = new ChartSeries();

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu_Chart_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);


        for (RPT_TongDoanhThuModel m : lst) {
           c.set(m.getCpName() +" "+ Utilities.formatNumber("#,###", m.getDoanhThuCP()), m.getDoanhThuCP());
        }
        return c;
    }
    
    
    
    public CartesianChartModel RPT_ThongKeDoanhThu_Chart_BIT_line_ngay() throws SQLException {
        CartesianChartModel c = new CartesianChartModel();
       
        
        List<RPT_TongDoanhThuModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu_Chart_BIT_line_ngay(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
            List<RPT_TongDoanhThuModel> lst1=DAL.executeQuery("Select distinct CPID,CPName from users_report where users_report.CPID<>'' order by users_report.CPID desc",RPT_TongDoanhThuModel.class);
            for(RPT_TongDoanhThuModel m:lst1)
            {
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Doanh thu " + m.getCpName());
                
                int i=0;
                boolean bln=false;
                
                //ChartSeries data=new ChartSeries();
                
                while(i<lst.size())
                {
                    if(!l.getData().containsKey(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM")))
                    {
                        l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getCpid() == null ? m.getCpid() == null : lst.get(i).getCpid().equals(m.getCpid()))
                    {
                        if(lst.get(i).getTongSoBit()>0)
                        {
                            l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), lst.get(i).getTongSoBit());
                            bln=true;
                            //l.setShowLine(true);
                        }
                    }
                   
                        
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
            
            LineChartSeries l1=new LineChartSeries();
            l1.setLabel("Tổng doanh thu");
            int i=0;
            while(i<lst.size())
            {

                  l1.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), lst.get(i).getTongCong());
                 l1.setShowLine(true);
                     i++;
            }
            
            
             c.addSeries(l1);
             
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            if(lst.size()>0)
                l2.setLabel("CP " + lst.get(0).getCpName());

            for (RPT_TongDoanhThuModel m : lst) {
               
                l2.set(Utilities.parseDateTime(m.getNgay(), "dd/MM"),(m.getTongSoBit())*30/100);
                l2.setShowLine(true);    

            }
             if(l2.getData().size()>0)
                c.addSeries(l2);
        }
        
        return c;
    }
     public CartesianChartModel RPT_ThongKeDoanhThu_Chart_BIT_line_thang() throws SQLException {
        CartesianChartModel c = new CartesianChartModel();
        List<RPT_TongDoanhThuModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu_Chart_BIT_line_thang(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
            List<RPT_TongDoanhThuModel> lst1=DAL.executeQuery("Select distinct CPID,CPName from users_report where users_report.CPID<>''",RPT_TongDoanhThuModel.class);
            for(RPT_TongDoanhThuModel m:lst1)
            {
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Doanh thu " + m.getCpName());
                int i=0;
                boolean bln=false;
                while(i<lst.size())
                {
                      if(!l.getData().containsKey(lst.get(i).getThang()))
                    {
                        l.set(lst.get(i).getThang(), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getCpid() == null ? m.getCpid() == null : lst.get(i).getCpid().equals(m.getCpid()))
                    {
                        if(lst.get(i).getTongSoBit()>0)
                        {
                            l.set(lst.get(i).getThang(), lst.get(i).getTongSoBit());
                            l.setShowLine(true);
                            bln=true;
                        }
                        
                    }
                   
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
            
            LineChartSeries l1=new LineChartSeries();
            l1.setLabel("Tổng doanh thu");
            int i=0;
            while(i<lst.size())
            {

                  l1.set(lst.get(i).getThang(), lst.get(i).getTongCong());
                 l1.setShowLine(true);
                     i++;
             
                  
            }
            
            
             c.addSeries(l1);
             
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            boolean bln=false;

            for (RPT_TongDoanhThuModel m : lst) {
               if(!bln)
               {
                   bln=true;
                   l2.setLabel("CP Name " + m.getCpName());
               }
                l2.set(m.getThang(),m.getTongSoBit()*30/100);
                l2.setShowLine(true);    
            }
             
             c.addSeries(l2);
        }
        
        return c;
    }
     
     public CartesianChartModel RPT_ThongKeDoanhThu_Chart_BAC_line_ngay() throws SQLException {
        CartesianChartModel c = new CartesianChartModel();
        List<RPT_TongDoanhThuModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu_Chart_BAC_line_ngay(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
            List<RPT_TongDoanhThuModel> lst1=DAL.executeQuery("Select distinct CPID,CPName from users_report where users_report.CPID<>''",RPT_TongDoanhThuModel.class);
            for(RPT_TongDoanhThuModel m:lst1)
            {
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Doanh thu " + m.getCpName());
                int i=0;
                boolean bln=false;
                while(i<lst.size())
                {
                     if(!l.getData().containsKey(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM")))
                    {
                        l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), 0);
                        l.setShowLine(true);
                    }
                     
                    if(lst.get(i).getCpid() == null ? m.getCpid() == null : lst.get(i).getCpid().equals(m.getCpid()))
                    {
                         l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), lst.get(i).getTongSoBit());
                         l.setShowLine(true);
                         bln=true;
                    }
                   
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
            
            LineChartSeries l1=new LineChartSeries();
            l1.setLabel("Tổng doanh thu");
            int i=0;
            while(i<lst.size())
            {

                  l1.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), lst.get(i).getTongCong());
                 l1.setShowLine(true);
                     i++;
             
                  
            }
            
            
             c.addSeries(l1);
             
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            
            boolean bln=false;
            for (RPT_TongDoanhThuModel m : lst) {
                if(!bln)
                {
                    l2.setLabel("CP name " + m.getCpName());
                    bln=true;
                }
                
                l2.set(Utilities.parseDateTime(m.getNgay(), "dd/MM"),m.getTongSoBit() * 20 / 100);
                l2.setShowLine(true);    

            }
             
             c.addSeries(l2);
        }
        
        return c;
    }
     public CartesianChartModel RPT_ThongKeDoanhThu_Chart_BAC_line_thang() throws SQLException {
        CartesianChartModel c = new CartesianChartModel();
        List<RPT_TongDoanhThuModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu_Chart_BAC_line_thang(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
            List<RPT_TongDoanhThuModel> lst1=DAL.executeQuery("Select distinct CPID,CPName from users_report where users_report.CPID<>''",RPT_TongDoanhThuModel.class);
            for(RPT_TongDoanhThuModel m:lst1)
            {
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Doanh thu " + m.getCpName());
                int i=0;
                boolean bln=false;
                while(i<lst.size())
                {
                     if(!l.getData().containsKey(lst.get(i).getThang()))
                    {
                        l.set(lst.get(i).getThang(), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getCpid() == null ? m.getCpid() == null : lst.get(i).getCpid().equals(m.getCpid()))
                    {
                         l.set(lst.get(i).getThang(), lst.get(i).getTongSoBit());
                         l.setShowLine(true);
                         bln=true;
                    }
                    
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
            
            LineChartSeries l1=new LineChartSeries();
            l1.setLabel("Tổng doanh thu");
            int i=0;
            while(i<lst.size())
            {

                  l1.set(lst.get(i).getThang(), lst.get(i).getTongCong());
                 l1.setShowLine(true);
                     i++;
             
                  
            }
            
            
             c.addSeries(l1);
             
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            
            boolean bln=false;
            
            for (RPT_TongDoanhThuModel m : lst) {
               if(!bln)
               {
                   l2.setLabel("CP Name " + m.getCpName());
                   bln=true;
               }
                l2.set(m.getThang(),m.getTongSoBit()* 20 / 100);
                l2.setShowLine(true);    

            }
             
             c.addSeries(l2);
        }
        
        return c;
    }
     
     
     public CartesianChartModel RPT_ThongKeDoanhThu_Chart_DoanhThu_line_ngay() throws SQLException {
        CartesianChartModel c = new CartesianChartModel();
        List<RPT_TongDoanhThuModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu_Chart_TongDoanhThu_line_ngay(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
            List<RPT_TongDoanhThuModel> lst1=DAL.executeQuery("Select distinct CPID,CPName from users_report where users_report.CPID<>''",RPT_TongDoanhThuModel.class);
            for(RPT_TongDoanhThuModel m:lst1)
            {
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Doanh thu " + m.getCpName());
                int i=0;
                boolean bln=false;
                while(i<lst.size())
                {
                      if(!l.getData().containsKey(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM")))
                    {
                        l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getCpid() == null ? m.getCpid() == null : lst.get(i).getCpid().equals(m.getCpid()))
                    {
                       
                         l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), lst.get(i).getPtBit()+lst.get(i).getTongSoBit());
                         l.setShowLine(true);
                         bln=true;
                    }
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
            
            LineChartSeries l1=new LineChartSeries();
            l1.setLabel("Tổng doanh thu");
            int i=0;
            while(i<lst.size())
            {
                if(!l1.getData().containsKey(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM")))
                {
                  l1.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), lst.get(i).getTongCong());
                 l1.setShowLine(true);
                }
                     i++;
             
                  
            }
            
            
             c.addSeries(l1);
             
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            l2.setLabel("CP Name ");
            boolean bln=false;

            for (RPT_TongDoanhThuModel m : lst) {
               if(!bln)
               {
                    l2.setLabel("CP Name " + m.getCpName());
                    bln=true;
               }
                l2.set(Utilities.parseDateTime(m.getNgay(), "dd/MM"),m.getDoanhThuCP());
                l2.setShowLine(true);    

            }
             
             c.addSeries(l2);
        }
        
        return c;
    }
     public CartesianChartModel RPT_ThongKeDoanhThu_Chart_DoanhThu_line_thang() throws SQLException {
        CartesianChartModel c = new CartesianChartModel();
        List<RPT_TongDoanhThuModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu_Chart_TongDoanhThu_line_thang(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
            List<RPT_TongDoanhThuModel> lst1=DAL.executeQuery("Select distinct CPID,CPName from users_report where users_report.CPID<>''",RPT_TongDoanhThuModel.class);
            for(RPT_TongDoanhThuModel m:lst1)
            {
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Doanh thu " + m.getCpName());
                int i=0;
                boolean bln=false;
                while(i<lst.size())
                {
                    if(!l.getData().containsKey(lst.get(i).getThang()))
                    {
                        l.set(lst.get(i).getThang(), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getCpid() == null ? m.getCpid() == null : lst.get(i).getCpid().equals(m.getCpid()))
                    {
                         l.set(lst.get(i).getThang(), lst.get(i).getPtBit()+lst.get(i).getTongSoBit());
                         l.setShowLine(true);
                         bln=true;
                    }
                   
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
            
            LineChartSeries l1=new LineChartSeries();
            l1.setLabel("Tổng doanh thu");
            int i=0;
            while(i<lst.size())
            {
                if(!l1.getData().containsKey(lst.get(i).getThang()))
                {
                    l1.set(lst.get(i).getThang(), lst.get(i).getTongCong());
                    l1.setShowLine(true);
                }
                    i++;
            }
            
            
             c.addSeries(l1);
             
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            boolean bln=false;
            for (RPT_TongDoanhThuModel m : lst) {
               if(!bln)
               {
                   l2.setLabel("CP Name " + m.getCpName());
                   bln=true;
               }
                l2.set(m.getThang(),m.getDoanhThuCP());
                l2.setShowLine(true);    

            }
             
             c.addSeries(l2);
        }
        
        return c;
    }
     

    public void cmd_RPT_ThongKeDoanhThu_Excel_Click(ActionEvent event) throws SQLException, ParseException {
        Map mapParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.setCpid(String.valueOf(mapParams.get("CPID")));
        this.setGameID(String.valueOf(mapParams.get("Game")));
        this.setLoaiThongKe(String.valueOf(mapParams.get("LoaiTK")));
        String strTuNgay = String.valueOf(mapParams.get("TuNgay"));
        String strDenNgay = String.valueOf(mapParams.get("DenNgay"));
        setTuNgay(Utilities.parseDate(strTuNgay, "dd/MM/yyyy"));
        setDenNgay(Utilities.parseDate(strDenNgay, "dd/MM/yyyy"));
        
        WEBParamCollection Params = WEBParamCollection.getInstance();
       Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_TongDoanhThuModel> lst;

        switch (this.getLoaiThongKe()) {
            case "BIT":
                lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);
                setFileExporter(controller.thongKeDoanhThuTuBitToExcel(tuNgay, denNgay, lst,getLoaiThongKe()));
                break;
            case "BAC":
                lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);
                setFileExporter(controller.thongKeDoanhThuTuBacToExcel(tuNgay, denNgay, lst,getLoaiThongKe()));
                break;
            default:
                 lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);
                setFileExporter(controller.thongKeDoanhThuToExcel(tuNgay, denNgay, lst,getLoaiThongKe()));
                break;
        }
        if (getFileExporter() != null) {
            try {
                Utilities.deleteFile(new File(controller.getLocalPath() + "/" + getFileExporter().getName()));
            } catch (IOException ex) {
                Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void cmd_RPT_ThongKeDoanhThu_ThongKe_Click(ActionEvent event) throws SQLException, ParseException {
        Map mapParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.setCpid(String.valueOf(mapParams.get("CPID")));
        this.setGameID(String.valueOf(mapParams.get("Game")));
        this.setLoaiThongKe(String.valueOf(mapParams.get("LoaiTK")));
        String strTuNgay = String.valueOf(mapParams.get("TuNgay"));
        String strDenNgay = String.valueOf(mapParams.get("DenNgay"));
        setTuNgay(Utilities.parseDate(strTuNgay, "dd/MM/yyyy"));
        setDenNgay(Utilities.parseDate(strDenNgay, "dd/MM/yyyy"));
        switch (this.getLoaiThongKe()) {
            case "BIT":
                this.setPage("/layout/Web/thongkedoanhthu_bit.xhtml");
                break;
            case "BAC":
                this.setPage("/layout/Web/thongkedoanhthu_bac.xhtml");
                break;
            default:
                if(Sys_NguoiDungController.findUser().isIsAdmin())
                    this.setPage("/layout/Web/thongkedoanhthu_ad.xhtml");
                else
                    this.setPage("/layout/Web/thongkedoanhthu_cp.xhtml");
                break;
        }
    }

    public void cmd_RPT_ThongKeDoanhThu_Chart_Click(ActionEvent event) throws SQLException, ParseException {
        Map mapParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.setCpid(String.valueOf(mapParams.get("CPID")));
        this.setGameID(String.valueOf(mapParams.get("Game")));
        this.setLoaiThongKe(String.valueOf(mapParams.get("LoaiDT")));
        String strTuNgay = String.valueOf(mapParams.get("TuNgay"));
        String strDenNgay = String.valueOf(mapParams.get("DenNgay"));
        setTuNgay(Utilities.parseDate(strTuNgay, "dd/MM/yyyy"));
        setDenNgay(Utilities.parseDate(strDenNgay, "dd/MM/yyyy"));
        //this.setPage("/layout/Chart/thongkedoanhthu.xhtml");
         this.setLoaiBD(String.valueOf(mapParams.get("Loai")));
        
         switch (this.getLoaiBD()) {
            case "2":
                this.setPage("/layout/Chart/thongkedoanhthu.xhtml");
                break;
            case "4":
                this.setPage("/layout/Chart/thongkedoanhthu_line_ngay.xhtml");
                break;
            case "5":
                this.setPage("/layout/Chart/thongkedoanhthu_line_thang.xhtml");
                break;
        }
    }

    public List<RPT_TongDoanhThuModel> RPT_TongDoanhThu() throws SQLException {
        List<RPT_TongDoanhThuModel> lst;
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(this.getCpid());
        Params.add(this.getGameID());
        Params.add(this.getLoaiThongKe());
        Params.add(this.getGiaTriTheNap());
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuModel.class);

        return lst;

    }

    /**
     * @return the fileExporter
     */
    public StreamedContent getFileExporter() {
        return fileExporter;
    }

    /**
     * @param fileExporter the fileExporter to set
     */
    public void setFileExporter(StreamedContent fileExporter) {
        this.fileExporter = fileExporter;
    }

    
}
